//
//  Dictionary.m
//  CEDict
//
//  Created by Eric Nadeau on 07-06-01.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "Dictionary.h"


@implementation Dictionary

-(Dictionary*) initWithEntries: (NSArray*) dictionaryEntries
{
	entries = dictionaryEntries;
	return self;
}

-(NSArray*) search: (NSString*) query
{
	NSMutableArray* retval = [NSMutableArray array];
	NSEnumerator* iterator = [entries objectEnumerator];
	Entry* line;
	
	while( line = [iterator nextObject] )
	{
		if( [line matches:query] )
		{
			[retval addObject:line];
		}
	}
		
	return retval;
}

+(Dictionary*) loadFromFile: (NSString*) filename
{
	NSMutableArray* entries = [NSMutableArray array];
	NSString* file = [NSString stringWithContentsOfFile:filename];
	NSArray* lines = [file componentsSeparatedByString:@"\n"];

	NSEnumerator* iter = [lines objectEnumerator];
	// Skip comments on first line
	[iter nextObject];
	
	NSString* line;
	while( line = [iter nextObject] )
	{
		Entry* word = [Parser parseLine:line];
		[entries addObject: word];
	}
	
	Dictionary* retval = [[Dictionary alloc] initWithEntries:entries];
	return retval;
}

@end
